using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class SendFailedHintsPanel : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "SendFailedHintsPanel";
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchSnsInfo;
		private object mObjectWatcher = new object();
		private static SendFailedHintsPanel sInstance;
		public List<SnsInfo> listInfos = new List<SnsInfo>();
		internal StackPanel itemContainer;
		private bool _contentLoaded;
		public event SendFailedHintsPanel.ListItemClick ItemClick;
		public SendFailedHintsPanel()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchSnsInfo == null)
			{
				this.mWatchSnsInfo = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnSnsInfo));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSINFO_ADD, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSINFO_DEL, this.mWatchSnsInfo);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_SNSINFO_MODIFY, this.mWatchSnsInfo);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchSnsInfo != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSINFO_ADD, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSINFO_DEL, this.mWatchSnsInfo);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_SNSINFO_MODIFY, this.mWatchSnsInfo);
				this.mWatchSnsInfo = null;
			}
		}
		private void OnSnsInfo(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			switch (evtArgs.mEventID)
			{
			case EventConst.ON_STORAGE_SNSINFO_ADD:
			{
				List<SnsInfo> listObject = evtArgs.getListObject<SnsInfo>();
				if (listObject == null)
				{
					return;
				}
				using (List<SnsInfo>.Enumerator enumerator = listObject.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsInfo current = enumerator.get_Current();
						if (current != null && current.nStatus == 4)
						{
							this.AddOne(current);
						}
					}
					return;
				}
				break;
			}
			case EventConst.ON_STORAGE_SNSINFO_DEL:
				break;
			case EventConst.ON_STORAGE_SNSINFO_MODIFY:
				goto IL_CE;
			default:
				return;
			}
			List<SnsInfo> listObject2 = evtArgs.getListObject<SnsInfo>();
			if (listObject2 == null)
			{
				return;
			}
			using (List<SnsInfo>.Enumerator enumerator2 = listObject2.GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					SnsInfo current2 = enumerator2.get_Current();
					if (current2 != null && current2.strUserName == AccountMgr.strUsrName)
					{
						this.RemoveOne(current2);
					}
				}
				return;
			}
			IL_CE:
			List<SnsInfo> listObject3 = evtArgs.getListObject<SnsInfo>();
			if (listObject3 != null)
			{
				using (List<SnsInfo>.Enumerator enumerator3 = listObject3.GetEnumerator())
				{
					while (enumerator3.MoveNext())
					{
						SnsInfo current3 = enumerator3.get_Current();
						if (current3 != null)
						{
							if (current3.nStatus == 4)
							{
								this.AddOne(current3);
							}
							else
							{
								if (current3.strUserName == AccountMgr.strUsrName)
								{
									this.RemoveOne(current3);
								}
							}
						}
					}
				}
			}
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
		}
		private void hintCtrl_ItemClick(object sender, object object_0)
		{
			TimelineDetailPage.GotoThisPage(object_0 as SnsInfo, null);
		}
		public static SendFailedHintsPanel GetInstance(bool bRemoveFromFather = false)
		{
			if (SendFailedHintsPanel.sInstance == null)
			{
				SendFailedHintsPanel.sInstance = new SendFailedHintsPanel();
				SendFailedHintsPanel.sInstance.Init();
			}
			else
			{
				if (bRemoveFromFather && SendFailedHintsPanel.sInstance.get_Parent() != null)
				{
					Panel panel = SendFailedHintsPanel.sInstance.get_Parent() as Panel;
					if (panel != null)
					{
						panel.get_Children().Remove(SendFailedHintsPanel.sInstance);
					}
				}
			}
			return SendFailedHintsPanel.sInstance;
		}
		public static void RemoveFromFather()
		{
			if (SendFailedHintsPanel.sInstance != null && SendFailedHintsPanel.sInstance.get_Parent() != null)
			{
				Panel panel = SendFailedHintsPanel.sInstance.get_Parent() as Panel;
				if (panel != null)
				{
					panel.get_Children().Remove(SendFailedHintsPanel.sInstance);
				}
			}
		}
		public void Init()
		{
			List<SnsInfo> errorSendingObjectList = SnsInfoMgr.getErrorSendingObjectList();
			SendFailedHintsPanel.sInstance.registerEvent();
			if (errorSendingObjectList == null)
			{
				return;
			}
			using (List<SnsInfo>.Enumerator enumerator = errorSendingObjectList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsInfo current = enumerator.get_Current();
					this.AddOne(current);
				}
			}
		}
		public static void ClearPanel()
		{
			if (SendFailedHintsPanel.sInstance == null)
			{
				return;
			}
			SendFailedHintsPanel.sInstance.unRegisterEvent();
			SendFailedHintsPanel.RemoveFromFather();
			SendFailedHintsPanel.sInstance = null;
		}
		public void AddOne(SnsInfo info)
		{
			if (info.nStatus != 4)
			{
				return;
			}
			if (Enumerable.FirstOrDefault<SnsInfo>(Enumerable.Where<SnsInfo>(this.listInfos, (SnsInfo snsInfo_0) => snsInfo_0.nLocalID == info.nLocalID)) == null)
			{
				this.listInfos.Add(info);
				SendFailedHintItem oneItem = SendFailedHintItem.GetOneItem(info);
				oneItem.ItemClick += new SendFailedHintItem.ListItemClick(this.hintCtrl_ItemClick);
				this.itemContainer.get_Children().Add(oneItem);
			}
		}
		public void RemoveOne(SnsInfo info)
		{
			SnsInfo snsInfo = Enumerable.FirstOrDefault<SnsInfo>(Enumerable.Where<SnsInfo>(this.listInfos, (SnsInfo snsInfo_0) => snsInfo_0.nLocalID == info.nLocalID));
			if (snsInfo != null)
			{
				int num = this.listInfos.IndexOf(snsInfo);
				if (num >= 0 && num < this.itemContainer.get_Children().get_Count())
				{
					SendFailedHintItem sendFailedHintItem = this.itemContainer.get_Children().get_Item(num) as SendFailedHintItem;
					sendFailedHintItem.ItemClick -= new SendFailedHintItem.ListItemClick(this.hintCtrl_ItemClick);
					this.itemContainer.get_Children().RemoveAt(num);
				}
				this.listInfos.Remove(snsInfo);
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/SendFailedHintsPanel.xaml", 2));
			this.itemContainer = (StackPanel)base.FindName("itemContainer");
		}
	}
}
